<?php
class UsernameField extends AutoCompleteField {
    function __construct($name, $title = null, $value = '', $maxLength = null, $form = null) {
        $sourceClass = 'Member';
        $sourceField = 'Username';

        parent::__construct($name, $title, $value, $maxLength, $form, $sourceClass, $sourceField);
    }
	
	function Type() {
		return parent::Type() . ' username';
	}
    
    function validate($validator) {
        $result = DB::query(sprintf("SELECT COUNT(*) FROM \"Member\" WHERE \"Username\" = '%s'", Convert::raw2sql($this->value)))->value();

        if(!$result) {
            $validator->validationError($this->name, _t('UsernameField.USERNAME_INVALID', "The username is invalid"));
            return false;
        }

        return true;
    }
}
?>
